package com.epfresh.supplier.adapter;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.CategoryOneEntity;

import java.util.List;

import multitype.OnRecyclerViewItemClickListener;

public class CategoryTopAdapter extends RecyclerView.Adapter<CategoryTopAdapter.ViewHolder> implements View.OnClickListener {

    private List<CategoryOneEntity> categoryOneEntityList;

    OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;

    public OnRecyclerViewItemClickListener getOnRecyclerViewItemClickListener() {
        return onRecyclerViewItemClickListener;
    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    public void setCategory(List<CategoryOneEntity> categoryOneEntityList) {
        this.categoryOneEntityList = categoryOneEntityList;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item_category_top, parent, false);
        view.setOnClickListener(this);
        return new ViewHolder(view);
    }


    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        CategoryOneEntity categoryOneEntity = categoryOneEntityList.get(position);

        holder.title.setText(categoryOneEntity.getName());
        holder.itemView.setTag(R.id.item_key_position,position);

        if (categoryOneEntity.isSelect()) {
            holder.flag.setVisibility(View.VISIBLE);
            holder.title.setTextColor(Color.parseColor("#e2382a"));
        }else {
            holder.flag.setVisibility(View.INVISIBLE);
            holder.title.setTextColor(Color.parseColor("#434343"));
        }
        ImageLoader.getInstance().displayImage(categoryOneEntity.getIconUrl(),holder.cover);
    }


    @Override public int getItemCount() {
        return categoryOneEntityList != null ? categoryOneEntityList.size() : 0;
    }

    @Override
    public void onClick(View v) {

        Object tag = v.getTag(R.id.item_key_position);
        if (tag != null && tag instanceof Integer){
            int position = (int) tag;

            if (onRecyclerViewItemClickListener != null && position < categoryOneEntityList.size()){
                onRecyclerViewItemClickListener.onItemClick(v,position,categoryOneEntityList.get(position));
            }
        }
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView cover;
        TextView title;
        View flag;


        ViewHolder(final View itemView) {
            super(itemView);
            cover = (ImageView) itemView.findViewById(R.id.iv_img);
            title = (TextView) itemView.findViewById(R.id.tv_name);
            flag = itemView.findViewById(R.id.v_flag);
        }
    }


}
